<%@ include file="../common/IncludeTop.jsp"%>

<div id="BackLink">
	<a href="main">Return to Main Menu</a>
</div>

<div id="Catalog">

<div id="Cart">

<script>
	$(function () {
		<c:forEach var="cartItem" items="${sessionScope.cart.cartItems}">

		$("[name=\"${cartItem.item.itemId}\"]").on("keyup",function () {

			var price = $("[name=\"${cartItem.item.listPrice}\"]").val();
			var totalPrice;
			var count = $("[name=\"${cartItem.item.itemId}\"]").val();

			totalPrice = count * price;
			var t = totalPrice;
			$("[name=\"${cartItem.total}\"]").replaceWith("<input type=\"text\" disabled=\"disabled\" name=\"${cartItem.total}\" id=\"${cartItem.total}\"" + "value=" + t  + "$ />");
		})

		</c:forEach>

	})
</script>

<h2>Shopping Cart</h2>
	<form action="UpdateCast" method="post">
		<table>
			<tr>
				<th><b>Item ID</b></th>
				<th><b>Product ID</b></th>
				<th><b>Description</b></th>
				<th><b>In Stock?</b></th>
				<th><b>Quantity</b></th>
				<th><b>List Price</b></th>
				<th><b>Total Cost</b></th>
				<th>&nbsp;</th>
			</tr>

			<c:if test="${sessionScope.cart.numberOfItems == 0}">
				<tr>
					<td colspan="8"><b>Your cart is empty.</b></td>
				</tr>
			</c:if>

			<c:forEach var="cartItem" items="${sessionScope.cart.cartItems}">
				<tr>
					<td>
						<a href="ViewItem?itemId=${cartItem.item.itemId}">${cartItem.item.itemId}</a>
					</td>
					<td>
						${cartItem.item.product.productId}
					</td>
					<td>
						${cartItem.item.attribute1} ${cartItem.item.attribute2}
						${cartItem.item.attribute3} ${cartItem.item.attribute4}
						${cartItem.item.attribute5} ${cartItem.item.product.name}
					</td>
					<td>
						${cartItem.inStock}
					</td>
					<td>
						<input type="text" name="${cartItem.item.itemId}" id="${cartItem.item.itemId}" value="${cartItem.quantity}" />
					</td>
					<td>
						<input type="text" disabled="disabled" name="${cartItem.item.listPrice}" id="${cartItem.item.listPrice}" value="${cartItem.item.listPrice}" />
					</td>
					<td>
						<input type="text" disabled="disabled" name="${cartItem.total}" id="${cartItem.total}" value = "${cartItem.total}$" />
					</td>
					<td>
						<a href="RemoveItem?workingItemId=${cartItem.item.itemId}" class="Button">Remove</a>
					</td>
				</tr>
			</c:forEach>
			<tr>
				<td colspan="7">
					<input type="submit" value="Update Cart"/>
				</td>
				<td>&nbsp;</td>
			</tr>
		</table>
	</form>

		<c:if test="${sessionScope.cart.numberOfItems > 0}">
			<a href="ViewOrderForm" class="Button">Proceed to Checkout</a>
		</c:if>
</div>

<div id="Separator">&nbsp;</div>
</div>

<%@ include file="../common/IncludeBottom.jsp"%>